<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <header th:replace="header::html"></header>

</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <input type="hidden" id="userId" name="userId" th:value="${myUser.userId}">
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span style="color: red">*</span>用户名
                    </label>
                    <div class="layui-input-block">
                        <input type="text" id="userName" th:value="${myUser.userName}" name="userName" lay-verify="required" placeholder="请输入用户名" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span style="color: red">*</span>昵称</label>
                    <div class="layui-input-block">
                        <input type="text" id="nickName" th:value="${myUser.nickName}" name="nickName" required="" placeholder="请输入昵称" lay-verify="nikename" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span style="color: red">*</span>手机
                    </label>
                    <div class="layui-input-block">
                        <input type="text" id="phone" th:value="${myUser.phone}" name="phone" required="" lay-verify="phone" placeholder="请输入手机号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        <span style="color: red">*</span>邮箱
                    </label>
                    <div class="layui-input-block">
                        <input type="text"  name="email" th:value="${myUser.email}"lay-verify="email" autocomplete="off" placeholder="请输入邮箱" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="1" title="激活" checked>
                        <input type="radio" name="status" value="0" title="禁用">
                </div>
                </div>
                <input type="text" id="roleId"  name="roleId" style="display:none;"/>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block show-role-container">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">部门</label>
                    <div class="layui-input-block">
                        <input type="number" id="deptId" th:value="${myUser.deptId}" name="deptId"  lay-verify="deptId" style="display:none;width: 0px" autocomplete="off" class="layui-input">
                        <ul id="dataTree" class="dtree" data-id="0" th:data-value="${myUser.deptId}"></ul>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">岗位</label>
                    <div class="layui-input-block">
                        <select name="jobIds" id="jobIds" lay-filter="jobIds"  xm-select="jobIds" xm-select-skin="normal">
                            <option th:each="job:${jobs}" th:value="${job.jobId}" th:text="${job.jobName}" th:selected="${job.flag}" th:disabled="${job.status == '1'}"></option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script>
    layui.use(['form','select','dtree','jquery'],function(){
        let form = layui.form;
        $ = layui.jquery;
        let dtree = layui.dtree;
        getAllRole(function(){
            layui.form.render('radio')
        });
        // 初始化树
        dtree.render({
            elem: "#dataTree",
            initLevel: "1",
            width: "100%",
            method: 'get',
            dataStyle: "layuiStyle",  //使用layui风格的数据格式
            response:{message:"msg",statusCode:200},  //修改response中返回数据的定义
            url: "/api/dept/build",
            dataFormat: "list",  //配置data的风格为list
            select: true, //指定下拉树模式
            selectCardHeight: "200",
        });
        var param = dtree.getNowParam("dataTree");
        formDate = $("#deptId");
        dtree.on("node('dataTree')" ,function(obj){
            var param = dtree.getNowParam("dataTree");
            $("#deptId").val(param.nodeId);
            formDate = $("#deptId");
        });
        form.on('submit(user-save)', function(data){
            var rolelRadio = $("[name=roleId]:checked");
            if(rolelRadio == undefined || rolelRadio.length <= 0){
                layer.alert("请给该用户设置角色");
                return false;
            }
            let roleIds = "";
            $('input[type=checkbox]:checked').each(function() {
                roleIds += $(this).val()+",";
            });
            roleIds = roleIds.substr(0,roleIds.length-1);
            data.field.roleIds = roleIds;
            var jobIds = data.field.jobIds;
            var jobId = jobIds.split(",");
            data.field.jobIds = jobId
            $.ajax({
                url:'/api/user',
                data:JSON.stringify(data.field),
                dataType:'json',
                contentType:'application/json',
                type:'post',
                success:function(result){
                    if(result.success){
                        layer.msg(result.msg,{icon:1,time:1000},function(){
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.location.reload();
                        });
                    }else{
                        layer.msg(result.msg,{icon:2,time:1000});
                    }
                }
            })
            return false;
        });
        //自定义验证规则
        form.verify({
            nikename: function(value){
                if(value.length < 3){
                    return '昵称至少得3个字符啊';
                }
            }
            ,phone: [/(.+){11,11}$/, '电话号码必须11位']
            ,email: [/^[a-z0-9._%-]+@([a-z0-9-]+\.)+[a-z]{2,4}$|^1[3|4|5|7|8]\d{9}$/, '邮箱格式不对']
        });
    })
    function getAllRole(callback){
        $.ajax({
            url:"/api/role/all",
            type:"GET",
            dataType:'json',
            success:function(result){
                var html = "";
                if(result.data && result.data.length > 0){
                    for(i in result.data){
                        html += '<input type="radio"  name="roleId" value="'+result.data[i].roleId+'" title="'+result.data[i].roleName+'" >' +
                            '<div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i><div>'+result.data[i].roleName+'</div></div>';
                    }
                    $(".show-role-container").html(html);
                    callback();
                }
            }
        });
    }
</script>
<script>
</script>
</body>
</html>